#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

void do_encrypt_rc4(const char *src, const char *passwd, char *output) {
    int i, j = 0, a = 0, b = 0, c = 0;
    int temp;
    int plen = strlen(passwd);
    int size = strlen(src);

    int key[256];
    int sbox[256];

    for (i = 0; i < 256; i++) {
        key[i] = passwd[i % plen];
        sbox[i] = i;
    }

    for (i = 0; i < 256; i++) {
        j = (j + sbox[i] + key[i]) % 256;
        temp = sbox[i];
        sbox[i] = sbox[j];
        sbox[j] = temp;
    }

    for (i = 0; i < size; i++) {
        a = (a + 1) % 256;
        b = (b + sbox[a]) % 256;
        temp = sbox[a];
        sbox[a] = sbox[b];
        sbox[b] = temp;
        c = (sbox[a] + sbox[b]) % 256;
        temp = src[i] ^ sbox[c];
        sprintf(output + i * 2, "%02x", temp);
    }
}

int main() {
    const char *src = "xxxx";
 //   const char *passwd = "SecretKey";
    int size = strlen(src) * 2 + 1;

    char str[55];
    // 定义一个time_t类型的变量来存储时间戳
    time_t timestamp;

    // 获取当前时间
    time(&timestamp);

    // 打印时间戳
    printf("Current timestamp - auth_tag: %ld\n", timestamp);

    sprintf(str, "%d", (unsigned long int)timestamp );

    char *output = (char *)malloc(size);
    memset(output, 0, size);

    do_encrypt_rc4(src, str, output);

    printf("pwd : %s\n", output);
    free(output);

    return 0;
}

